package main

import (
	"context"
	"svvenus/conf"
	"svvenus/vgame/logic"
	"svvenus/vgame/route"
	"svvenus/vpub"
	"svvenus/vpub/constants"
	"svvenus/vpub/serve"
	"svvenus/vpub/worker"
	"svvenus/zlog"
	"time"
)

func main() {
	if !vpub.InitLead() {
		return
	}

	if !conf.InitMapAll() {
		return
	}
	if !conf.InitExtGame() {
		return
	}

	//游戏服服务器读档
	logic.ServerLoad()

	serves()

	workers()

	loops()

	//select {}
}

func serves() {
	//记录服务
	go serve.AddServe(serve.NewRecordServe())

	//http服务
	go serve.AddServe(serve.NewHttpServe(constants.PortHttp, route.HttpRegister()))
}

func workers() {
	//tcp服务
	go worker.AddWorker(worker.NewTcpWorker(route.TcpConnStart, route.TcpConnStop), route.TcpRegister)

	//读取命令行输入并执行
	go worker.AddWorker(worker.NewCmdlineWorker(), route.CmdRegister)

	//读取命令行输入并执行
	go worker.AddWorker(worker.NewScheduleWorker(), route.ScheduleRegister)
}

func loops() {
	vpub.Ctx, vpub.Cancel = context.WithCancel(context.Background())

	for {
		constants.SvTime = time.Now()
		time.Sleep(time.Millisecond * 10)

		select {
		case req := <-worker.TcpChan:
			worker.TcpMsgHandleDo(req)
		case schedule := <-worker.ScheduleChan:
			worker.ScheduleExec(schedule)
		case cmd := <-worker.CmdlineChan:
			worker.CmdlineExec(cmd)
		case <-vpub.Ctx.Done():
			zlog.Warn("Ctx.Done()")
			return
		default:
		}
	}
}
